using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class TimeSystem : SingletonMono<TimeSystem>
{
    private float bulletTimeValue = 0.1f;                           // 子弹时间流速

    private float timer;                                            // 计时器

    private float fixerUpdateTimeBulletTime;                        // 储存FixedUpdateTime的值
    private void Awake()
    {
        fixerUpdateTimeBulletTime = Time.fixedDeltaTime;
    }

    //

    /// <summary>
    /// 暂停游戏
    ///  true 是暂停
    /// </summary>
    /// <param name="value"></param>
    public void TakeGameTime(bool value)
    {
        Time.timeScale = value ?  0f : 1f;
    }

    /// <summary>
    /// 进入子弹时间并停留
    /// </summary>
    /// <param name="duration">进入时间</param>
    /// <param name="durationOut">离开时间</param>
    /// <param name="stayBulletTime">持续时间</param>
    public void TakeBulletTime(float duration, float durationOut, float stayBulletTime)
    {
        StartCoroutine(EnterStayBulletTime(duration, stayBulletTime));
    }

    /// <summary>
    /// 进入或离开
    /// </summary>
    /// <param name="duration">进出耗时</param>
    /// <param name="value">进出标识</param>
    public void TakeBulletTime(float duration, bool value)
    {
        if(value)
        {
            StartCoroutine(EnterBulletTime(duration));
        }
        else
        {
            StartCoroutine(ExitBulletTime(duration));
        }
            
    }

    /// <summary>
    /// 进和出
    /// </summary>
    /// <param name="durationIn">进入耗时</param>
    /// <param name="durationOut">离开耗时</param>
    public void TakeBulletTime(float durationIn, float durationOut)
    {
        StartCoroutine(EnterAndExitBulletTime(durationIn, durationOut));
    }


    
    /// <summary>
    /// 进入子弹时间
    /// </summary>
    /// <param name="duration"></param>
    /// <returns></returns>
    IEnumerator EnterBulletTime(float duration)
    {
        timer = 0;
        while(timer < 1f)
        {
            timer += Time.unscaledDeltaTime / duration;
            Time.timeScale = Mathf.Lerp(1f, bulletTimeValue, timer);
            Time.fixedDeltaTime = fixerUpdateTimeBulletTime * Time.timeScale;

            yield return null;
        }
    }

    /// <summary>
    /// 离开子弹时间
    /// </summary>
    /// <param name="duration"></param>
    /// <returns></returns>
    IEnumerator ExitBulletTime(float duration)
    {
        timer = 0;
        while(timer < 1f)
        {
            timer += Time.unscaledDeltaTime / duration;
            Time.timeScale = Mathf.Lerp(bulletTimeValue, 1f, timer);
            Time.fixedDeltaTime = fixerUpdateTimeBulletTime * Time.timeScale;

            yield return null;
        }
    }

    /// <summary>
    /// 进入和离开
    /// </summary>
    /// <param name="duration">进入持续时间</param>
    /// <returns></returns>
    IEnumerator EnterAndExitBulletTime(float durationIn, float durationOut)
    {
        yield return StartCoroutine(EnterBulletTime(durationIn));

        StartCoroutine(ExitBulletTime(durationOut));
    }

    IEnumerator EnterStayBulletTime(float duration, float stayBulletTime)
    {
        yield return StartCoroutine(EnterBulletTime(duration));

        yield return new WaitForSecondsRealtime(stayBulletTime);

        StartCoroutine(ExitBulletTime(duration));
    }
}
